import { showText } from "../../utils/util";
import { getExamQuestions, submitAnswer, QuestionType } from "@/api/task/questionnaire.service";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    CharCode: new Array(26).fill().map((item, index) => String.fromCharCode(index + 65)),
    QuestionType,
    examInfo: {},
    questionList: [],
    answerList: [],
    submitAble: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getExamQuestions(options.id).then(res => {
      if (res.code == 200) {
        console.log("qDataList", res.data);
        const questionList = res.data.questions;
        this.setData({
          examInfo: res.data.appExam,
          questionList,
          answerList: questionList.map(item => {
            return {
              aeId: item.aeId,
              aeqId: item.aeqId,
              answerContent: '',
              answerOptions: new Array(item.questionOptions?.length).fill(undefined)
            }
          })
        })
      } else showText("问卷内容有误", res.msg);
    });
  },

  onClickOption(e) {
    const data = e.currentTarget.dataset;
    console.log(data);
    const newAnswerList = [...this.data.answerList];
    if (data.type == "DANXT") {
      newAnswerList[data.index].answerOptions.fill(undefined);
      newAnswerList[data.index].answerOptions[data.oindex] = data.oitem;
    } else if (data.type == "DUOXT") {
      if (newAnswerList[data.index].answerOptions[data.oindex]) newAnswerList[data.index].answerOptions[data.oindex] = undefined;
      else newAnswerList[data.index].answerOptions[data.oindex] = data.oitem;
    }
    console.log("new answer", newAnswerList[data.index].answerOptions);
    this.setData({
      answerList: newAnswerList
    })
  },

  onInput(e) {
    const newAnswerList = [...this.data.answerList];
    newAnswerList[e.target.dataset.index].answerContent = e.detail.value;
    // console.log("new answer", newAnswerList[e.target.dataset.index]);
    this.setData({
      answerList: newAnswerList
    })
  },

  submit() {
    if (!this.data.submitAble) return;
    const _this = this;
    this.setData({ submitAble: false });
    wx.showLoading();
    // console.log(this.data.answerList);

    //表单验证&&数据处理
    for (let i = 0; i < this.data.questionList.length; i++) {
      const question = this.data.questionList[i];
      const answer = this.data.answerList[i];
      // console.log("第" + i + "题", question, answer, question.questionType);
      if ((question.questionType == 'DANXT' || question.questionType == 'DUOXT') && !answer.answerOptions.filter(item => item).length) {
        this.formFailTip(i + 1); return;
      } else if (question.questionType == 'WDT' && !answer.answerContent.trim()) {
        this.formFailTip(i + 1);
        return;
      }
    }

    //通过验证 处理答案
    const newAnswerList = this.data.answerList.map(item => {
      return {
        ...item,
        answerOptions: item.answerOptions.filter(item => item)
      }
    })
    // console.log("验证通过 最终提交data", newAnswerList);

    //提交答案
    submitAnswer({ answerList: newAnswerList }).then((res) => {
      if (res.code == 200) {
        wx.showModal({
          title: '提示',
          content: `提交成功，获得${this.data.examInfo.integral}积分！`,
          showCancel: false, // 是否显示取消按钮
          // confirmText: '确定', // 确定按钮的文字
          success(res) {
            if (res.confirm) wx.navigateBack();
            //  else if (res.cancel)
          },
        });
        this.setData({ submitAble: true });
        wx.hideLoading();
      } else {
        wx.showModal({ content: res.msg, showCancel: false });
        this.setData({ submitAble: true });
        wx.hideLoading();
      }
    });

  },

  formFailTip(i) {
    wx.showModal({ content: `请完成第${i}题`, showCancel: false });
    this.setData({ submitAble: true });
    wx.hideLoading();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})